﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Newtonsoft.Json.Linq;
using 批量注册;

namespace dns批量处理
{
    public partial class eDNS : Office2007Form
    {
        public eDNS()
        {
            InitializeComponent();
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView3.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.Columns[0].FillWeight = 15;
            dataGridView1.Columns[1].FillWeight = 30;
            dataGridView1.Columns[2].FillWeight = 55;

            buttonX2.Enabled = false;
            buttonX3.Enabled = false;
            buttonX4.Enabled = false;
            buttonX5.Enabled = false;
            checkBox1.Checked = true;
            checkBox2.Checked = true;
            checkBox3.Checked = true;
            radioButton1.Checked = true;
            txtMX.Text = "5";
            AddToRichtextbox("请先登录\r\n");
            getImage();
        }
        string defaultmailcookie;
        private CookieContainer cco; //存储登陆后需要的cookie
        private void getImage()
        {
            try
            {
                GetHtmlString("http://www.edns.cn", out defaultmailcookie);
                CookieContainer coc = new CookieContainer();
                //填充需要发送的cookie
                coc.SetCookies(new Uri("http://www.edns.cn"), defaultmailcookie);
                GetLoginImage("http://www.edns.cn/login/verifyCode", "http://www.edns.cn", coc);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw ex;
            }

        }
        /// <summary>
        /// get方法请求 根据URL获得返回的页面字符串与cookie
        /// </summary>
        /// <param name="URL">访问的页面地址</param>
        /// <param name="cookie">返回的cookie</param>
        /// <returns>返回页面的字符串数据</returns>
        private string GetHtmlString(string URL, out string cookie)
        {
            HttpWebRequest httpWebRequest;
            HttpWebResponse webResponse;

            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
           // httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.Accept = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
            httpWebRequest.Method = "Get";

            httpWebRequest.UserAgent =
                "User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 BIDUBrowser/6.x Safari/537.36";
            webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            string strhtml = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd();
            cookie = webResponse.Headers.Get("Set-Cookie");
            return strhtml;
        }
        /// <summary>
        /// 得到登录的验证码
        /// </summary>
        /// <param name="URL"></param>
        /// <param name="referUrl"></param>
        /// <param name="coc"></param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        private Bitmap GetLoginImage(string URL, string referUrl, CookieContainer coc)
        {
            HttpWebRequest httpWebRequest;
            HttpWebResponse webResponse;

            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
          //  httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.Accept = "Accept:image/webp,*/*;q=0.8";
            httpWebRequest.Method = "Get";
            httpWebRequest.Referer = referUrl;
            httpWebRequest.UserAgent =
                "User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 BIDUBrowser/6.x Safari/537.36";
            httpWebRequest.CookieContainer = coc;
            webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            Stream resStream = webResponse.GetResponseStream();//得到验证码数据流
            Bitmap sourcebm = new Bitmap(resStream);//初始化Bitmap图片
            //  string strhtml = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd();
            pictureBox1.Image = sourcebm;
            //   cookie = webResponse.Headers.Get("Set-Cookie");
            return sourcebm;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Enabled)
            {
                getImage();
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text == "" || txtPassword.Text == "" || txtYanZheng.Text == "")
            {
                MessageBox.Show("请输入用户名或密码");
                return;
            }
            try
            {
                if (login(out cco))
                {
                    buttonX1.Enabled = false;
                    buttonX2.Enabled = true;
                    buttonX3.Enabled = true;
                    buttonX4.Enabled = true;
                    buttonX5.Enabled = true;
                    txtUserName.Enabled = false;
                    txtPassword.Enabled = false;
                    txtYanZheng.Enabled = false;
                    pictureBox1.Enabled = false;
                    AddToRichtextbox("登陆成功！\r\n");
                }
                else
                {
                    MessageBox.Show("登陆失败！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }
        }
        private bool login(out CookieContainer coc2)
        {


            //进行登录
            string postData = "uEmail=" + txtUserName.Text.Trim() + "&uPassword=" + txtPassword.Text.Trim() + "&verifyCode=" + txtYanZheng.Text.Trim();
            string authloginCookie;
            CookieContainer coc1 = new CookieContainer();
            //填充需要发送的cookie
            coc1.SetCookies(new Uri("http://www.edns.cn"), defaultmailcookie);
            string posthtml = PostHtmlString("http://www.edns.cn/auth/login ", "http://www.edns.cn", coc1, postData,
                out authloginCookie);
            JObject jo = JObject.Parse(posthtml);

            if (jo["status"].ToString() == "True")
            {
                txtUserName.Enabled = false;
                txtPassword.Enabled = false;

                //返回认证完成的cookie
                coc2 = new CookieContainer();
                coc2.SetCookies(new Uri("http://www.edns.cn"), defaultmailcookie);
                coc2.SetCookies(new Uri("http://www.edns.cn"), authloginCookie);
                //coc2.Add(new Uri("http://www.edns.cn"),
                //    new Cookie("peach", System.Web.HttpUtility.UrlEncode(txtUserName.Text.Trim())));
                return true;
            }
            else
            {
                coc2 = null;
                return false;
            }
        }
        /// <summary>
        /// 得到post请求返回的页面字符串并得到返回的cookie
        /// </summary>
        /// <param name="URL"></param>
        /// <param name="referUrl"></param>
        /// <param name="coc"></param>
        /// <param name="postdata">向页面传递的字符串数据</param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        private string PostHtmlString(string URL, string referUrl, CookieContainer coc, string postdata,
            out string cookie)
        {
            HttpWebRequest httpWebRequest;
            HttpWebResponse webResponse;

            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
            httpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            httpWebRequest.Accept = "application/json, text/javascript, */*; q=0.01";
            httpWebRequest.Method = "Post";
            httpWebRequest.Referer = referUrl;
            httpWebRequest.Headers.Set("X-Requested-With", "XMLHttpRequest");
            httpWebRequest.UserAgent =
                "User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 BIDUBrowser/6.x Safari/537.36";
            httpWebRequest.CookieContainer = coc;
            byte[] byteRequest = Encoding.Default.GetBytes(postdata);
            //添加post数据
            httpWebRequest.ContentLength = byteRequest.Length;
            Stream stream;
            stream = httpWebRequest.GetRequestStream();
            stream.Write(byteRequest, 0, byteRequest.Length);
            stream.Close();

            webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            string strhtml = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd();
            cookie = webResponse.Headers.Get("Set-Cookie");
            return strhtml;
        }
        /// <summary>
        /// 得到post请求返回的页面字符串并得到返回的cookie
        /// </summary>
        /// <param name="URL"></param>
        /// <param name="referUrl"></param>
        /// <param name="coc"></param>
        /// <param name="postdata">向页面传递的字符串数据</param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        private string PostHtmlString(string URL, string referUrl, CookieContainer coc, string postdata)
        {
            HttpWebRequest httpWebRequest;
            HttpWebResponse webResponse;

            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
            httpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            httpWebRequest.Accept = "application/json, text/javascript, */*; q=0.01";
            httpWebRequest.Method = "Post";
            httpWebRequest.Referer = referUrl;
            httpWebRequest.Headers.Set("X-Requested-With", "XMLHttpRequest");
            httpWebRequest.UserAgent =
                "User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 BIDUBrowser/6.x Safari/537.36";
            httpWebRequest.CookieContainer = coc;
            byte[] byteRequest = Encoding.Default.GetBytes(postdata);
            //添加post数据
            httpWebRequest.ContentLength = byteRequest.Length;
            Stream stream;
            stream = httpWebRequest.GetRequestStream();
            stream.Write(byteRequest, 0, byteRequest.Length);
            stream.Close();

            webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            string strhtml = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd();
            //cookie = webResponse.Headers.Get("Set-Cookie");
            return strhtml;
        }
        public delegate void DelegateAddToRichtextbox(string str); //委托相当于一个描述函数的地址

        private void AddToRichtextbox(string str)
        {
            if (this.InvokeRequired)
            {
                DelegateAddToRichtextbox a = new DelegateAddToRichtextbox(AddToRichtextbox);
                this.BeginInvoke(a, str); //这里调用的委托,其是在主线程内的,其声明实在主函数中声明的,
            }
            else
            {
                richTextBox1.AppendText(str);
            }
            //  Thread.Sleep(10);
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            txtUserName.Enabled = true;
            txtPassword.Enabled = true;
            txtYanZheng.Enabled = true;
            pictureBox1.Enabled = true;
            buttonX1.Enabled = true;
            buttonX2.Enabled = false;
            buttonX3.Enabled = false;
            buttonX4.Enabled = false;
            buttonX5.Enabled = false;
            getImage();
        }

        private void buttonX7_Click(object sender, EventArgs e)
        {
            dataGridView2.Rows.Clear();
        }

        private void buttonX12_Click(object sender, EventArgs e)
        {
            dataGridView3.Rows.Clear();
        }

        private void buttonX8_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                StreamReader m_streamReader = new StreamReader(openFileDialog1.FileName, System.Text.Encoding.Default);
                string s;
                while ((s = m_streamReader.ReadLine()) != null)
                {
                    int index = dataGridView2.Rows.Add();
                    dataGridView2.Rows[index].Cells[0].Value = s;
                }
            }
        }

        private void buttonX11_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                StreamReader m_streamReader = new StreamReader(openFileDialog1.FileName, System.Text.Encoding.Default);
                string s;
                while ((s = m_streamReader.ReadLine()) != null)
                {
                    int index = dataGridView3.Rows.Add();
                    dataGridView3.Rows[index].Cells[0].Value = s;
                }
            }
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            if (dataGridView2.Rows.Count != dataGridView3.Rows.Count)
            {
                MessageBox.Show("域名与IP数目不一致，请检查");
                return;
            }

            Thread tr = new Thread(new ThreadStart(bangding));
            tr.Start();
        }
        private bool DomainToIPOnly(CookieContainer coc, string domainName, out string domainId)
        {
            string postdata = "isMark=1&domain=" + domainName +
                              "&gId=1";
            string cookietmp;
            coc.Add(new Uri("http://www.edns.cn"),
                    new Cookie("show", System.Web.HttpUtility.UrlEncode("yes")));
            string strResponse = PostHtmlString("http://www.edns.cn/API/domain/create",
                "http://www.edns.cn/domain", coc, postdata, out cookietmp);
            JObject jo = JObject.Parse(strResponse);
            domainId = "";
            if ((string)jo["msg"] == "域名添加成功")
            {
                domainId = (string)jo["data"];
                return true;
            }
            else
            {
                MessageBox.Show((string)jo["msg"]);
                return false;
            }
        }
        private bool DomainToIpType(CookieContainer coc, string IP, string domainId, int type)
        {
            string postdata;
            switch (type)
            {
                case 1:
                    postdata = "dId=" + domainId +
                               "&subDomain=www&ttl=600&record_type=1&record_line=1&value=" + IP;
                    break;
                case 2:
                    postdata = "dId=" + domainId +
                               "&subDomain=%40&ttl=600&record_type=1&record_line=1&value=" + IP;
                    ;
                    break;
                case 3:
                    postdata = "dId=" + domainId +
                               "&subDomain=*&ttl=600&record_type=1&record_line=1&value=" + IP;
                    ;
                    break;
                case 4:
                    postdata = "dId=" + domainId +
                               "&subDomain=www&ttl=600&record_type=2&record_line=1&value=" + IP;
                    break;
                case 5:
                    postdata = "dId=" + domainId +
                               "&subDomain=%40&ttl=600&record_type=2&record_line=1&value=" + IP;
                    ;
                    break;
                case 6:
                    postdata = "dId=" + domainId +
                               "&subDomain=*&ttl=600&record_type=2&record_line=1&value=" + IP
                               ;
                    ;
                    break;
                case 7:
                    postdata = "dId=" + domainId +
                               "&subDomain=www&ttl=600&record_type=3&record_line=1&extra=" +
                               txtMX.Text.Trim() + "&value=" + IP;
                    break;
                case 8:
                    postdata = "dId=" + domainId +
                               "&subDomain=%40&ttl=600&record_type=3&record_line=1&extra=" +
                               txtMX.Text.Trim() + "&value=" + IP;
                    ;
                    break;
                case 9:
                    postdata = "dId=" + domainId +
                               "&subDomain=*&ttl=600&record_type=3&record_line=1&extra=" +
                               txtMX.Text.Trim() + "&value=" + IP;
                    ;
                    break;
                default:
                    MessageBox.Show("error in DomainToIpType");
                    return false;
            }

            string cookietmp;
            string strResponse = PostHtmlString("http://www.edns.cn/API/record/create",
                "http://www.edns.cn/domain", coc, postdata, out cookietmp);
            if (strResponse == null)
            {
                MessageBox.Show("未接收到有效数据");
                return false;
            }
            JObject jo = JObject.Parse(strResponse);

            if ((string)jo["status"] == "True")
            {
                return true;
            }
            else
            {
                MessageBox.Show((string)jo["msg"]);
                return false;
            }
        }
        private void bangding()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("域名");
            dt.Columns.Add("ip");
            dataGridView2.EndEdit();
            dataGridView3.EndEdit();
            try
            {
                int inum = dataGridView2.Rows.Count;//因为datagridview中的行数随时在改变，如果用循环将导致循环的不准确
                for (int i = 0; i < inum - 1; i++)
                {
                    string domainName = dataGridView2.Rows[0].Cells[0].Value.ToString();
                    string ip = dataGridView3.Rows[0].Cells[0].Value.ToString();
                    string domainId;
                    if (DomainToIPOnly(cco, domainName, out domainId))
                    {
                        AddToRichtextbox(domainName + " 添加成功\r\n");
                    }
                    else
                    {
                        MessageBox.Show("添加记录失败！");
                        return;
                    }
                    if (checkBox1.Checked == true && radioButton1.Checked == true)
                    {
                        if (DomainToIpType(cco, ip, domainId, 1))
                        {
                            AddToRichtextbox(domainName + " 添加www记录成功，类型 A IP：" + ip + "\r\n");
                        }
                        else
                        {
                            MessageBox.Show("添加www记录失败！");
                            return;
                        }
                    }
                    if (checkBox2.Checked == true && radioButton1.Checked == true)
                    {
                        if (DomainToIpType(cco, ip, domainId, 2))
                        {
                            AddToRichtextbox(domainName + " 添加@记录成功，类型 A IP：" + ip + "\r\n");
                        }
                        else
                        {
                            MessageBox.Show("添加@记录失败！");
                            return;
                        }
                    }
                    if (checkBox3.Checked == true && radioButton1.Checked == true)
                    {
                        if (DomainToIpType(cco, ip, domainId, 3))
                        {
                            AddToRichtextbox(domainName + " 添加*记录成功，类型 A IP：" + ip + "\r\n");
                        }
                        else
                        {
                            MessageBox.Show("添加*记录失败！");
                            return;
                        }
                    }

                    if (checkBox1.Checked == true && radioButton2.Checked == true)
                    {
                        if (DomainToIpType(cco, ip, domainId, 4))
                        {
                            AddToRichtextbox(domainName + " 添加www记录成功，类型 CNAME 记录值：" + ip + "\r\n");
                        }
                        else
                        {
                            MessageBox.Show("添加www记录失败！");
                            return;
                        }
                    }
                    if (checkBox2.Checked == true && radioButton2.Checked == true)
                    {
                        if (DomainToIpType(cco, ip, domainId, 5))
                        {
                            AddToRichtextbox(domainName + " 添加@记录成功，类型 CNAME 记录值：" + ip + "\r\n");
                        }
                        else
                        {
                            MessageBox.Show("添加@记录失败！");
                            return;
                        }
                    }
                    if (checkBox3.Checked == true && radioButton2.Checked == true)
                    {
                        if (DomainToIpType(cco, ip, domainId, 6))
                        {
                            AddToRichtextbox(domainName + " 添加*记录成功，类型 CNAME 记录值：" + ip + "\r\n");
                        }
                        else
                        {
                            MessageBox.Show("添加*记录失败！");
                            return;
                        }
                    }
                    if (checkBox1.Checked == true && radioButton3.Checked == true)
                    {
                        if (DomainToIpType(cco, ip, domainId, 7))
                        {
                            AddToRichtextbox(domainName + " 添加www记录成功，类型 MX 记录值：" + ip + "\r\n");
                        }
                        else
                        {
                            MessageBox.Show("添加www记录失败！");
                            return;
                        }
                    }
                    if (checkBox2.Checked == true && radioButton3.Checked == true)
                    {
                        if (DomainToIpType(cco, ip, domainId, 8))
                        {
                            AddToRichtextbox(domainName + " 添加@记录成功，类型 MX 记录值：" + ip + "\r\n");
                        }
                        else
                        {
                            MessageBox.Show("添加@记录失败！");
                            return;
                        }
                    }
                    if (checkBox3.Checked == true && radioButton3.Checked == true)
                    {
                        if (DomainToIpType(cco, ip, domainId, 9))
                        {
                            AddToRichtextbox(domainName + " 添加*记录成功，类型 MX 记录值：" + ip + "\r\n");
                        }
                        else
                        {
                            MessageBox.Show("添加*记录失败！");
                            return;
                        }
                    }
                    deleteRowFromDatagridview();
                    Thread.Sleep(1500);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }

            MessageBox.Show("已完成操作");

        }
        public delegate void DelegatedeleteRowFromDatagridview();

        private void deleteRowFromDatagridview()
        {
            if (this.InvokeRequired)
            {
                DelegatedeleteRowFromDatagridview d = new DelegatedeleteRowFromDatagridview(deleteRowFromDatagridview);
                this.BeginInvoke(d);
            }
            else
            {
                dataGridView2.Rows.RemoveAt(0);
                dataGridView3.Rows.RemoveAt(0);
                dataGridView2.Update();
                dataGridView3.Update();
            }
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            DataTable tmpdt = GetDomainList(cco);
            dataGridView1.AutoGenerateColumns = false; //这句话非常重要
            if (tmpdt != null)
            {
                dataGridView1.DataSource = tmpdt;
                dataGridView1.Columns[1].DataPropertyName = tmpdt.Columns[0].ToString();
                dataGridView1.Columns[2].DataPropertyName = tmpdt.Columns[1].ToString();
            }


        }
        /// <summary>
        /// 得到登陆账户下已添加的域名
        /// </summary>
        /// <param name="coc"></param>
        /// <param name="xsrf"></param>
        /// <returns></returns>
        private DataTable GetDomainList(CookieContainer coc)
        {
            string postdata =
                "pagetype=recent&pageNum=0";
            //string html1 = PostHtmlString("http://www.edns.cn/API/domaingroup/glist", "http://www.edns.cn/domain",
            //    coc, "");
            //string html2 = PostHtmlString("http://www.edns.cn/API/domaingroup/listName", "http://www.edns.cn/domain",
            //    coc, "");
            string html = PostHtmlString("http://www.edns.cn/API/domain/pageList", "http://www.edns.cn/domain",
                coc, postdata);
            JObject jo = JObject.Parse(html);
            if (jo["status"].ToString() != "True")
            {
                MessageBox.Show("读取错误", "提示", MessageBoxButtons.OK);
                return null;
            }
            JArray jlist = JArray.Parse(jo["data"]["domains"].ToString());
            DataTable dt = new DataTable();
            dt.Columns.Add("domainId");
            dt.Columns.Add("name");
            //dt.Columns.Add("records");
            //dt.Columns.Add("updated_on");
            //dt.Columns.Add("grade");
            for (int i = 0; i < jlist.Count; i++)
            {
                DataRow dr = dt.NewRow();
                JObject tmpjo = JObject.Parse(jlist[i].ToString());
                dr["domainId"] = tmpjo["dId"].ToString();
                dr["name"] = tmpjo["domain"].ToString();
                //dr["records"] = tmpjo["records"].ToString();
                //dr["updated_on"] = tmpjo["updated_on"].ToString();
                //dr["grade"] = tmpjo["grade"].ToString();
                dt.Rows.Add(dr);
            }
            return dt;
        }
        private bool DeleteDomain(CookieContainer coc, string domainId)
        {
            string postdata = "dId=" + domainId;
            //   string cookietmp;
            string strResponse = PostHtmlString("http://www.edns.cn/API/domain/remove",
                "http://www.edns.cn/domain", coc, postdata);
            JObject jo = JObject.Parse(strResponse);

            if (jo["status"].ToString() == "True")
            {
                return true;
            }
            else
            {
                //MessageBox.Show((string)jo["status"]["message"]);
                return false;
            }
        }
        private void buttonX5_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("是否删除所选择的域名？", "确认", MessageBoxButtons.YesNo))
            {
                try
                {
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true)
                        {
                            if (DeleteDomain(cco, dataGridView1.Rows[i].Cells[1].Value.ToString()))
                            {
                                AddToRichtextbox("已删除域名" + dataGridView1.Rows[i].Cells[2].Value.ToString() + "\r\n");
                            }
                            else
                            {
                                MessageBox.Show("删除失败");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    throw;
                }

            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {
                if ((bool)dataGridView1.Rows[e.RowIndex].Cells[0].EditedFormattedValue == false)
                {
                    dataGridView1.Rows[e.RowIndex].Cells[0].Value = true;
                }
                else
                {
                    dataGridView1.Rows[e.RowIndex].Cells[0].Value = false;
                }
            }
        }

        private void buttonX9_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == false)
                {
                    dataGridView1.Rows[i].Cells[0].Value = true;
                }
            }
        }

        private void buttonX10_Click(object sender, EventArgs e)
        {

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true)
                {
                    dataGridView1.Rows[i].Cells[0].Value = false;
                }
                else
                {
                    dataGridView1.Rows[i].Cells[0].Value = true;
                }
            }
        }

        private void buttonX6_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
        }

        private void buttonX13_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog(); //实例化一个保存对话框
            sf.Filter = @"txt文件(*.txt)|*.txt|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "";
            sf.FileName = "所选域名";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") //如果没有输入文件名
                {
                    return;
                }
                try
                {
                    StreamWriter sw = null; //定义一个文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.Unicode);
                    // sw.WriteLine("字段1\t\t字段2\t\t字段3\t\t字段4"); //写入表中的标题
                    for (int i = 0; i < dataGridView1.Rows.Count; i++) //循环写入第一个表里面的数据
                    {
                        if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true)
                        {
                            for (int j = 1; j < dataGridView1.Columns.Count; j++)
                            {
                                str = dataGridView1.Rows[i].Cells[j].Value.ToString();
                                sw.Write(str + "\t");
                            }
                            sw.WriteLine("");
                        }
                    }
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("数据导出成功！", "提示：");
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        private void dataGridView2_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            labelX3.Text = "共 " + (dataGridView2.RowCount - 1).ToString() + " 条";
        }

        private void dataGridView2_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            labelX3.Text = "共 " + (dataGridView2.RowCount - 1).ToString() + " 条";
        }

        private void dataGridView3_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            labelX4.Text = "共 " + (dataGridView2.RowCount - 1).ToString() + " 条";
        }

        private void dataGridView3_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            labelX4.Text = "共 " + (dataGridView2.RowCount - 1).ToString() + " 条";
        }

        private void eDNS_FormClosing(object sender, FormClosingEventArgs e)
        {
           // this.Hide();
          //  e.Cancel = true;
        }

    }
}
